package tni;

import java.util.Enumeration;
import java.util.Hashtable;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mlabatino
 */
public class Util {
    private Hashtable<Character,Integer> alphabet;
    private Hashtable<Integer,Integer> pixel;
    private Data dataT;
    private Data dataI;

    public Util(Data data1, Data data2){
        //Init table
        this.alphabet = new Hashtable<Character, Integer>();
        this.pixel = new Hashtable<Integer, Integer>();
        this.dataT = data1;
        this.dataI = data2;
    }

    public Hashtable<Character, Integer> getAlphabet() {
        return alphabet;
    }

    public Hashtable<Integer, Integer> getPixel() {
        return pixel;
    }

    /** Fonction de construction de l'alphabet **/
    public void foundOccurText(){
        for(int i=0; i<dataT.size(); i++)
            checkChar((Character)dataT.get(i));
    }
    public void checkChar(Character c){
        if(alphabet.containsKey(c))
            alphabet.put(c,(alphabet.get(c)+1));
        else
            alphabet.put(c,1);
    }

    /** Fonction de l'image **/
    public void foundOccurImage(){
        for(int i=0; i<dataI.size(); i++)
            checkPixel((Integer)dataI.get(i));
    }
    public void checkPixel(Integer i){
        if(pixel.containsKey(i))
            pixel.put(i,(pixel.get(i)+1));
        else
            pixel.put(i,1);
    }

    /** Fonction d'affichage **/
    public void affTextOccur(){
        System.out.println("== Affichage du tableau d'occurence ==");
        Enumeration<Character> keys = alphabet.keys();
        Enumeration<Integer> elements = alphabet.elements();
        while(keys.hasMoreElements()){
            Integer occ = elements.nextElement();
            System.out.println("Caractère : "+keys.nextElement()+" - Nombre d'occurence : "+occ+" - Pourcentage: "+floor(calculProbaT(occ))*100+"%");
        }
    }

    /** Fonction mathematique **/
    public double floor(float nb){
        double out = nb*10000+0.5;
        out = Math.floor(out);
        return out/10000;
    }
    public double log2(float nb){
        return Math.log(nb)/Math.log(2);
    }
    public float calculProbaT(Integer occ){
        return ((float)occ/dataT.size());
    }
    public float calculProbaI(Integer occ){
        return ((float)occ/dataI.size());
    }
    public float calculTextEntropy(){
        float out = 0;
        Enumeration<Integer> elements = alphabet.elements();
        while(elements.hasMoreElements()){
            float proba = calculProbaT(elements.nextElement());
            out -= proba*log2(proba);
        }
        return out;
    }
    public float calculImageEntropy(){
        float out = 0;
        Enumeration<Integer> elements = pixel.elements();
        while(elements.hasMoreElements()){
            float proba = calculProbaI(elements.nextElement());
            out -= proba*log2(proba);
        }
        return out;
    }

    /** Affichage entropy **/
    public void affTextEntropy(){
        System.out.println("== Affichage de l'entropy du text ==");
        System.out.println(calculTextEntropy());
    }
    public void affImageEntropy(){
        System.out.println("== Affichage de l'entropy de l'image ==");
        System.out.println(calculImageEntropy());
    }
}
